menu_pointer = 0

menu_show_flag = True
'''
menu_pointer_maxline = {
    "start": 3
    "debug": 8
    "image": 3
    }
'''
menu_level_pointer = 0#菜单层级

menu_debug = [0.0,0.0,0.0,0.0,0.0,0.0,0,0]
menu_debug_vir = [True,True ,True ,True ,True ,True,True,True,False]


def show_start(lcd):#显示开始菜单
    lcd.str16(0,0,"debug",0x07E0)
    lcd.str16(0,16,"image",0x07E0)
    lcd.str16(0,32,"run",0x07E0)
    lcd.str16(150,16*menu_pointer ,"PP",0x07E0)
def show_debug(lcd):
    global menu_debug,menu_pointer
    lcd.str16(0,0,"G_P:{:>03f}".format(menu_debug[0]),0x07E0)
    lcd.str16(0,16,"G_D:{:>03f}".format(menu_debug[1]),0x07E0)
    lcd.str16(0,32,"A_P:{:>03f}".format(menu_debug[2]),0x07E0)
    lcd.str16(0,48,"A_D:{:>03f}".format(menu_debug[3]),0x07E0)
    lcd.str16(0,64,"S_P:{:>03f}".format(menu_debug[4]),0x07E0)
    lcd.str16(0,80,"S_D:{:>03f}".format(menu_debug[5]),0x07E0)
    lcd.str16(0,96,"pitch:{:>03f}".format(menu_debug[6]),0x07E0)
    lcd.str16(0,112,"step:{:>03f}".format(menu_debug[7]),0x07E0)
    lcd.str16(0,128,"BACK",0x07E0)
    
    lcd.str16(150,16*menu_pointer ,"PP",0x07E0)
def show_image(lcd,ccd1_data,ccd2_data):
    lcd.wave(0, 0,128, 64, ccd1_data, max = 4095)
    lcd.wave(0, 64,128, 64, ccd2_data, max = 4095)

def show_menu(lcd,ccd1_data,ccd2_data):
    global menu_show_flag
    """
    菜单显示
    lcd : lcd对象 
    """
    
    if menu_show_flag:
        if(menu_level_pointer == 0):
        #显示三行
      
            show_start(lcd)
        elif(menu_level_pointer == 1):
        # 显示参数
        # back
        # menulevel状态转移
      
            show_debug(lcd)
        
        elif(menu_level_pointer == 2): 
        ##显示图像
   
            show_image(lcd,ccd1_data,ccd2_data)
        
        
def key_handler(keynum,lcd):
    global menu_level_pointer, menu_pointer,menu_show_flag ,menu_debug ,menu_debug_vir
    lcd.clear()
    
    if(keynum == 1):
        if(menu_level_pointer == 0):
            menu_pointer = (menu_pointer - 1)%3
        elif(menu_level_pointer == 1):
             menu_pointer = (menu_pointer - 1)%9
        elif(menu_level_pointer == 2):
            menu_pointer = (menu_pointer - 1)%3
            
    elif(keynum == 0):
        if(menu_level_pointer == 0):
            menu_pointer = (menu_pointer + 1)%3
        elif(menu_level_pointer == 1):
             menu_pointer = (menu_pointer + 1)%9
        elif(menu_level_pointer == 2):
            menu_pointer = (menu_pointer + 1)%3
    
    elif(keynum == 2):
        
        if(menu_level_pointer == 0):
            #开始菜单
            if(menu_pointer == 0):
                #进入debug菜单
                menu_pointer = 0
                menu_level_pointer = 1
            elif(menu_pointer == 1):
                #进入image菜单
                menu_pointer  = 0
                menu_level_pointer = 2
            elif(menu_pointer == 2):
                #开始运行
                menu_level_pointer = 3
                menu_show_flag = False
        
        elif(menu_level_pointer == 1):
            if(menu_debug_vir[menu_pointer]):#该行的参数是否可变
                if(menu_pointer != 7):#该行是否是步长设置
                    menu_debug[menu_pointer] += menu_debug[7]
                elif(menu_pointer == 7):#步长设置
                    menu_debug[menu_pointer] += 0.1
            else:#该行不可改变，back
                menu_level_pointer = 0
                menu_pointer = 0
        elif(menu_level_pointer == 2):
            #显示图像返回
            menu_level_pointer = 0
            menu_pointer = 0
    
    elif(keynum == 3):
         if(menu_level_pointer == 1):
            if(menu_debug_vir[menu_pointer]):#该行的参数是否可变
                if(menu_pointer != 7):#该行是否是步长设置
                    menu_debug[menu_pointer] -= menu_debug[7]
                elif(menu_pointer == 7):#步长设置
                    menu_debug[menu_pointer] -= 0.1
            else:
                menu_level_pointer = 0
                menu_pointer = 0

def getshowflag():
    global menu_show_flag
    return menu_show_flag

def setdebugmenu(py,dy,pa,da,ps,ds,pitch):
    menu_debug[0] = py
    menu_debug[1] = dy
    menu_debug[2] = pa
    menu_debug[3] = da
    menu_debug[4] = ps
    menu_debug[5] = ds
    menu_debug[6] = pitch

def getdebugmenu():
    global menu_debug
    return (menu_debug[0],menu_debug[1],menu_debug[2],menu_debug[3],menu_debug[4],menu_debug[5])